<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 系统Style加载组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Styles extends Cola
{
	//系统CSS
	protected static $styles = array('');
	
    public static $template = '<link rel="stylesheet" href="%s">';
	
    public function set($string, $name = NULL, $after = NULL)
    {
		if(0 === strpos($string, 'http') || 0 === strpos($string, '/')){
			$string = sprintf(self::$template, $string);
		} else {
			$string = '<style type="text/css">'. $string . '</style>';
		}
		
		if(NULL !== $after && isset(self::$styles[$after])){
			!is_string($name) ? self::$styles[$after][] = $string : self::$styles[$after][$name] = $string;
		} else {
			!is_string($name) ? self::$styles[] = $string : self::$styles[$name][$name] = $string;
		}
		return $this;
    }
	
	
    public function render()
    {
		$html = '';
        foreach (self::$styles as $styles) 
		{
			if (is_array($styles)){
				foreach ($styles as $style) $html .=  $style;
			} else
            $html .=  $styles;
        }
		return $html;
    }
}
